//
//  DNFeedingDao.m
//  DanoneEA
//
//  Created by 3ti_Jery on 16/10/19.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNFeedingDao.h"
#import "DNUserManager.h"

@implementation DNFeedingDao
+ (NSString *) getCreateSQL
{
    return [NSString
            stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ text primary key,%@ text,%@ text,%@ integer,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text);",
            Feeding_TABLE,
            DairyBase_AppId,
            DairyBase_ServerId,
            DairyBase_InfantId,
            DairyBase_Status,
            DairyBase_ModifyReason,
            DairyBase_DeleteReason,
            DairyBase_CreateTime,
            DairyBase_ModifyTime,
            DairyBase_DeleteTime,
            DairyBase_Ddat,
            Feeding_feedingId,
            Feeding_numOfStuFor,
            Feeding_numOfBreFee,
            Feeding_numOfOthForFee,
            Feeding_numOfBOTWOT,
            Feeding_numOfComFee,
            Feeding_timesOfDay,
            Feeding_feedScoop,
            Feeding_feedPrep,
            Feeding_feedLeft,
            Feeding_feedCon
            
            ];
    
}

+ (NSString *) getInsertSQL
{
    return [NSString stringWithFormat:@"REPLACE INTO %@(%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
            Feeding_TABLE,
            DairyBase_AppId,
            DairyBase_ServerId,
            DairyBase_InfantId,
            DairyBase_Status,
            DairyBase_ModifyReason,
            DairyBase_DeleteReason,
            DairyBase_CreateTime,
            DairyBase_ModifyTime,
            DairyBase_DeleteTime,
            DairyBase_Ddat,
            Feeding_feedingId,
            Feeding_numOfStuFor,
            Feeding_numOfBreFee,
            Feeding_numOfOthForFee,
            Feeding_numOfBOTWOT,
            Feeding_numOfComFee,
            Feeding_timesOfDay,
            Feeding_feedScoop,
            Feeding_feedPrep,
            Feeding_feedLeft,
            Feeding_feedCon
            ];
    
}

#pragma mark - add
/**
 *  add
 *
 */
- (BOOL)saveFeedModel:(DNFeedModel *) object
{
    DNDataBase *dataBase = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dataBase.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        ret =[db executeUpdate:[DNFeedingDao getInsertSQL],
              object.appId,
              object.serverId,
              object.infantId,
              object.status,
              object.modifyReason,
              object.deleteReason,
              object.createTime,
              object.modifyTime,
              object.deleteTime,
              object.ddat,
              object.feedingId,
              object.numOfStuFor,
              object.numOfBreFee,
              object.numOfOthForFee,
              object.numOfBOTWOT,
              object.numOfComFee,
              object.TimeOfDay,
              object.feedScoop,
              object.feedPrep,
              object.feedLeft,
              object.feedCon
              ];
        if (!ret) {
            ret = NO;
            [db rollback];;
            DDLogError(@"Update error AppId:%@",
                       object.appId);
            [db close];
            return;
        }
        [db close];
    }];
    
    return ret;
    
}

- (BOOL)saveFeedModelWithArray:(NSArray *)objectList
{
    __block BOOL ret = YES;
    DNDataBase *dataBase = [DNDataBase shareInstance];
    
    // 如果要支持事务
    [dataBase.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        [db setKey:DB_SECRETKEY];
        
        for (DNFeedModel *object in objectList) {
            
            ret =[db executeUpdate:[DNFeedingDao getInsertSQL],
                  object.appId,
                  object.serverId,
                  object.infantId,
                  object.status,
                  object.modifyReason,
                  object.deleteReason,
                  object.createTime,
                  object.modifyTime,
                  object.deleteTime,
                  object.ddat,
                  object.feedingId,
                  object.numOfStuFor,
                  object.numOfBreFee,
                  object.numOfOthForFee,
                  object.numOfBOTWOT,
                  object.numOfComFee,
                  object.TimeOfDay,
                  object.feedScoop,
                  object.feedPrep,
                  object.feedLeft,
                  object.feedCon];
            
            if (!ret) {
                ret = NO;
                *rollback = YES;
                DDLogError(@"Update error AppId:%@",
                           object.appId);
                return;
            }
        }
        
    }];
    
    return ret;
    
}

#pragma mark - delete
/**
 *  delete
 *
 */
- (BOOL)deleteFeedModelByAppId:(NSString *)appId
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = %@;",Feeding_TABLE,DairyBase_AppId,appId];
        ret = [db executeUpdate:deleteSQL];
        [db close];
        
    }];
    return ret;
    
}

#pragma mark - update

/**
 *  update
 *
 */
- (BOOL)updateFeedModel:(DNFeedModel *)object
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * updateString = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' ,%@ = '%@',%@ = %@,%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = %@,%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@' WHERE %@ = '%@'",
                                   Feeding_TABLE,
                                   DairyBase_ServerId,object.serverId,
                                   DairyBase_InfantId,object.infantId,
                                   DairyBase_Status,object.status,
                                   DairyBase_ModifyReason,object.modifyReason,
                                   DairyBase_DeleteReason,object.deleteReason,
                                   DairyBase_CreateTime,object.createTime,
                                   DairyBase_ModifyTime,object.modifyTime,
                                   DairyBase_DeleteTime,object.deleteTime,
                                   DairyBase_Ddat,object.ddat,
                                   Feeding_feedingId,object.feedingId,
                                   Feeding_numOfStuFor,object.numOfStuFor,
                                   Feeding_numOfBreFee,object.numOfBreFee,
                                   Feeding_numOfOthForFee,object.numOfOthForFee,
                                   Feeding_numOfBOTWOT,object.numOfBOTWOT,
                                   Feeding_numOfComFee,object.numOfComFee,
                                   Feeding_timesOfDay,object.TimeOfDay,
                                   Feeding_feedScoop,object.feedScoop,
                                   Feeding_feedPrep,object.feedPrep,
                                   Feeding_feedLeft,object.feedLeft,
                                   Feeding_feedCon,object.feedCon,
                                   DairyBase_AppId,object.appId
                                   ];
        ret = [db executeUpdate:updateString];
        [db close];
    }];
    return ret;
    
}

- (BOOL)updateFeedModelByAppId:(NSString *)appId
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        DNFeedModel *object = [self getObjectModelByAppId:appId];
        
        NSString * updateString = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'",
                                   Feeding_TABLE,
                                   DairyBase_Status,object.status,
                                   DairyBase_AppId,object.appId
                                   
                                   ];
        
        ret = [db executeUpdate:updateString];
        [db close];
    }];
    return ret;
    
}

#pragma mark - query

/**
 *  check
 *
 */
- (DNFeedModel *)getObjectModelByAppId:(NSString *)appId
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block  DNFeedModel *object = nil;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = %@",Feeding_TABLE,DairyBase_AppId,appId]];
        
        while ([rs next]) {
            
            object = [[DNFeedModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setFeedingId:[rs stringForColumn:Feeding_feedingId]];
            [object setNumOfStuFor:[rs stringForColumn:Feeding_numOfStuFor]];
            [object setNumOfBreFee:[rs stringForColumn:Feeding_numOfBreFee]];
            [object setNumOfOthForFee:[rs stringForColumn:Feeding_numOfOthForFee]];
            [object setNumOfBOTWOT:[rs stringForColumn:Feeding_numOfBOTWOT]];
            [object setNumOfComFee:[rs stringForColumn:Feeding_numOfComFee]];
            [object setTimeOfDay:[rs stringForColumn:Feeding_timesOfDay]];
            [object setFeedScoop:[rs stringForColumn:Feeding_feedScoop]];
            [object setFeedPrep:[rs stringForColumn:Feeding_feedPrep]];
            [object setFeedLeft:[rs stringForColumn:Feeding_feedLeft]];
            [object setFeedCon:[rs stringForColumn:Feeding_feedCon]];
        }
        [db close];
    }];
    
    return object;
    
}

- (NSMutableArray *)getAllObjectModelNotSynchronized
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *synchronousDays = [[DNUserManager sharedInstance] getValueFromDefaultWithKey:UserDefaultKey_SynchronousDays];
        
        NSString * queryString =  [self getQueryAllModelSqlWithTableName:Feeding_TABLE andBeforeDays:synchronousDays.intValue];
        FMResultSet *rs = [db executeQuery:queryString];
        while ([rs next]) {
            DNFeedModel *object = [[DNFeedModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setFeedingId:[rs stringForColumn:Feeding_feedingId]];
            [object setNumOfStuFor:[rs stringForColumn:Feeding_numOfStuFor]];
            [object setNumOfBreFee:[rs stringForColumn:Feeding_numOfBreFee]];
            [object setNumOfOthForFee:[rs stringForColumn:Feeding_numOfOthForFee]];
            [object setNumOfBOTWOT:[rs stringForColumn:Feeding_numOfBOTWOT]];
            [object setNumOfComFee:[rs stringForColumn:Feeding_numOfComFee]];
            [object setTimeOfDay:[rs stringForColumn:Feeding_timesOfDay]];
            [object setFeedScoop:[rs stringForColumn:Feeding_feedScoop]];
            [object setFeedPrep:[rs stringForColumn:Feeding_feedPrep]];
            [object setFeedLeft:[rs stringForColumn:Feeding_feedLeft]];
            [object setFeedCon:[rs stringForColumn:Feeding_feedCon]];
            
            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close];
    }];
    
    return items;
    
    
}

- (NSMutableArray *)getAppDisplayObjectModel
{
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * queryString =  [self getQueryAppDisplayModelSqlWithTableName:Feeding_TABLE];
        FMResultSet *rs = [db executeQuery:queryString];
        while ([rs next]) {
            DNFeedModel *object = [[DNFeedModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setFeedingId:[rs stringForColumn:Feeding_feedingId]];
            [object setNumOfStuFor:[rs stringForColumn:Feeding_numOfStuFor]];
            [object setNumOfBreFee:[rs stringForColumn:Feeding_numOfBreFee]];
            [object setNumOfOthForFee:[rs stringForColumn:Feeding_numOfOthForFee]];
            [object setNumOfBOTWOT:[rs stringForColumn:Feeding_numOfBOTWOT]];
            [object setNumOfComFee:[rs stringForColumn:Feeding_numOfComFee]];
            [object setTimeOfDay:[rs stringForColumn:Feeding_timesOfDay]];
            [object setFeedScoop:[rs stringForColumn:Feeding_feedScoop]];
            [object setFeedPrep:[rs stringForColumn:Feeding_feedPrep]];
            [object setFeedLeft:[rs stringForColumn:Feeding_feedLeft]];
            [object setFeedCon:[rs stringForColumn:Feeding_feedCon]];
            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close];
    }];
    
    return items;
    
}


@end
